﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_Default : System.Web.UI.Page
{
    DataTable DTPartners;
    DataTable DTLinks;
    

    protected void Page_Load(object sender, EventArgs e)
    {

        GetData();

        this.Page.ClientScript.RegisterOnSubmitStatement(
   this.GetType(),
   "AjaxHack",
   "for ( var i = 0; i < parent.frames.length; ++i ) if ( parent.frames[i].FCK ) parent.frames[i].FCK.UpdateLinkedField();"
   );
    }

    private void GetData()
    {
        
            Dal.arParams.Clear();
            Dal.arParams.Add(0); // Partners information
            DTPartners = Dal.ExecuteDBsp("usp_GetFooterLinksByType", Dal.arParams);

            Dal.arParams.Clear();
            Dal.arParams.Add(1); // Links information
            DTLinks = Dal.ExecuteDBsp("usp_GetFooterLinksByType", Dal.arParams);

            gvPartners.DataSource = DTPartners;
            gvPartners.DataBind();

            gvLinks.DataSource = DTLinks;
            gvLinks.DataBind();

        // GetInformation
        if (!IsPostBack)
        {
            DataTable DT = Dal.ExecuteDBsp("usp_GetInformation");
            if (DT != null)
                if (DT.Rows.Count > 0)
                {
                    edInformation.Value = DT.Rows[0]["Context"].ToString();
                    edCopyright.Value = DT.Rows[1]["Context"].ToString();
                }
             txtKeywords.Text=PageHeaderData.GetPageKeywordsByType(NewsType.Default);
             CategoryItem cat=Category.ItemById((int)NewsType.Default);
             txtDescription.Text = cat.cDescription;
             txtBrowserTitle.Text = cat.cTitle;

             DT = Dal.ExecuteDBsp("Admin_GetHPStaticText");
             if (DT != null)
                 if (DT.Rows.Count > 0)
                 {
                     txtHPTextTitle.Text= DT.Rows[0]["Title"].ToString();
                     txtHPTextContent.Value = DT.Rows[0]["Content"].ToString();
                 }
        }
    }

    protected void btnAddDescription_Click(Object sender, EventArgs e)
    {
        PageHeaderData.SetPageKeywords(NewsType.Default, txtKeywords.Text);

        Dal.arParams.Clear();
        Dal.arParams.Add((int)NewsType.Default);
        Dal.arParams.Add(txtBrowserTitle.Text);
        Dal.arParams.Add(txtDescription.Text);
        Dal.ExecuteDBsp("Admin_AddCategoryDescription", Dal.arParams, true);
    }


    protected void btnEditPartner_Click(Object sender, CommandEventArgs e)
    {
        int iLinkID;
        Int32.TryParse(e.CommandArgument.ToString(), out iLinkID);

        Dal.arParams.Clear();
        Dal.arParams.Add(iLinkID);

        DataTable DT = Dal.ExecuteDBsp("Admin_GetFooterLinkByID", Dal.arParams);

        if (DT != null)
            if (DT.Rows.Count > 0)
            {
                hfAddPartner.Value = DT.Rows[0]["ID"].ToString();
                txtAddPartnerText.Text = DT.Rows[0]["LinkText"].ToString();
                txtAddPartnerLink.Text = DT.Rows[0]["LinkURL"].ToString();
            }
    }

    protected void gvPartners_ItemCommand(object source, CommandEventArgs e)
    {
        bool bChanged = false;
        Int32 rowID;
        Int32.TryParse(e.CommandArgument.ToString(), out rowID);
        Int32 rowIndex = 0;

        //if (Convert.ToBoolean(ViewState["pnlManageVisible"]))
        //{
        //    ViewState["pnlManageVisible"] = false;
        //    pnlManage.Controls.Clear();
        //    pnlManage.Style.Add("display", "none");
        //}
        if (DTPartners != null)
        {
            for (Int16 i = 0; i < DTPartners.Rows.Count; i++)
            {
                if (DTPartners.Rows[i]["ID"].Equals(rowID))
                {
                    rowIndex = i;
                    break;
                }
            }

            switch (e.CommandName)
            {
                case "MoveUp":
                    if (rowIndex > 0)
                    {
                        DTPartners.Rows[rowIndex - 1]["orderID"] = rowIndex;
                        DTPartners.Rows[rowIndex]["orderID"] = rowIndex - 1;
                        bChanged = true;
                    }
                    break;
                case "MoveDown":
                    if (rowIndex < DTPartners.Rows.Count - 1)
                    {
                        DTPartners.Rows[rowIndex + 1]["orderID"] = rowIndex;
                        DTPartners.Rows[rowIndex]["orderID"] = rowIndex + 1;
                        bChanged = true;
                    }
                    break;
            }
            if (bChanged)
            {
                string sXml = string.Empty;
                for (int iCount = 0; iCount < DTPartners.Rows.Count; iCount++)
                {
                    sXml += string.Format("<c id=\"{0}\" ord=\"{1}\"/>", DTPartners.Rows[iCount]["ID"], DTPartners.Rows[iCount]["orderID"]);
                }
                sXml = "<cs>" + sXml + "</cs>";

                Dal.arParams.Clear();
                Dal.arParams.Add(sXml);
                Dal.ExecuteDBsp("Admin_UpdateOrderID", Dal.arParams);

            }
        }
        GetData();
        //Session["FilesOrder"] = dt;
    }

    protected void btnAddPartnerLink_Click(object sender, EventArgs e)
    {
        int iLinkID;
        Int32.TryParse(hfAddPartner.Value, out iLinkID);

        UpdateLink(iLinkID, 0, txtAddPartnerText.Text.Trim(), txtAddPartnerLink.Text.Trim(), false);
        txtAddPartnerText.Text = string.Empty;
        txtAddPartnerLink.Text = string.Empty;
    }

    protected void btnEditLink_Click(Object sender, CommandEventArgs e)
    {
        int iLinkID;
        Int32.TryParse(e.CommandArgument.ToString(), out iLinkID);

        Dal.arParams.Clear();
        Dal.arParams.Add(iLinkID);

        DataTable DT = Dal.ExecuteDBsp("Admin_GetFooterLinkByID", Dal.arParams);

        if (DT != null)
            if (DT.Rows.Count > 0)
            {
                hfLinks.Value = DT.Rows[0]["ID"].ToString();
                txtAddLinkText.Text = DT.Rows[0]["LinkText"].ToString();
                txtAddLinkURL.Text = DT.Rows[0]["LinkURL"].ToString();
            }
    }

    protected void btnDeletePartner_Click(Object sender, CommandEventArgs e)
    {
        int iLinkID;
        Int32.TryParse(e.CommandArgument.ToString(), out iLinkID);

        UpdateLink(iLinkID, 0, "", "", true);
    }

    protected void btnDeleteLink_Click(Object sender, CommandEventArgs e)
    {
        int iLinkID;
        Int32.TryParse(e.CommandArgument.ToString(), out iLinkID);

        UpdateLink(iLinkID, 1, "", "", true);
    }

    protected void btnAddLink_Click(object sender, EventArgs e)
    {
        int iLinkID;
        Int32.TryParse(hfAddPartner.Value, out iLinkID);

        UpdateLink(iLinkID, 1, txtAddLinkText.Text.Trim(), txtAddLinkURL.Text.Trim(), false);
        txtAddLinkText.Text = string.Empty;
        txtAddLinkURL.Text = string.Empty;
    }

    protected void gvLinks_ItemCommand(object source, CommandEventArgs e)
    {
        bool bChanged = false;
        Int32 rowID;
        Int32.TryParse(e.CommandArgument.ToString(), out rowID);
        Int32 rowIndex = 0;

        //if (Convert.ToBoolean(ViewState["pnlManageVisible"]))
        //{
        //    ViewState["pnlManageVisible"] = false;
        //    pnlManage.Controls.Clear();
        //    pnlManage.Style.Add("display", "none");
        //}
        if (gvLinks.Rows.Count>0)
        {
            for (Int16 i = 0; i < DTLinks.Rows.Count; i++)
            {
                if (DTLinks.Rows[i]["ID"].Equals(rowID))
                {
                    rowIndex = i;
                    break;
                }
            }

            switch (e.CommandName)
            {
                case "MoveUp":
                    if (rowIndex > 0)
                    {
                        DTLinks.Rows[rowIndex - 1]["orderID"] = rowIndex;
                        DTLinks.Rows[rowIndex]["orderID"] = rowIndex - 1;
                        bChanged = true;
                    }
                    break;
                case "MoveDown":
                    if (rowIndex < gvLinks.Rows.Count - 1)
                    {
                        DTLinks.Rows[rowIndex + 1]["orderID"] = rowIndex;
                        DTLinks.Rows[rowIndex]["orderID"] = rowIndex + 1;
                        bChanged = true;
                    }
                    break;
            }
            if (bChanged)
            {
                string sXml = string.Empty;
                for (int iCount = 0; iCount < DTLinks.Rows.Count; iCount++)
                {
                    sXml += string.Format("<c id=\"{0}\" ord=\"{1}\"/>", DTLinks.Rows[iCount]["ID"], DTLinks.Rows[iCount]["orderID"]);
                }
                sXml = "<cs>" + sXml + "</cs>";

                Dal.arParams.Clear();
                Dal.arParams.Add(sXml);
                Dal.ExecuteDBsp("Admin_UpdateOrderID", Dal.arParams);

            }
        }
        GetData();
        //Session["FilesOrder"] = dt;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="iType">0 - partners , 1 - links</param>
    /// <param name="sText"></param>
    /// <param name="sLink"></param>
    private void UpdateLink(int iLinkID,int iType ,string sText,string sLink,bool Delete)
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(iLinkID); 
        Dal.arParams.Add(iType); 
        Dal.arParams.Add(sText);
        Dal.arParams.Add(sLink);
        Dal.arParams.Add(Delete);
        Dal.ExecuteDBsp("Admin_AddFooterLink", Dal.arParams,true);
        GetData();
    }
    protected void btnHPTextSave_Click(object sender, EventArgs e)
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(txtHPTextTitle.Text);
        Dal.arParams.Add(txtHPTextContent.Value);
        DataTable DT = Dal.ExecuteDBsp("Admin_UpdateHPStaticText", Dal.arParams, true);
    }

    protected void btnInfoSave_Click(object sender, EventArgs e)
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(1);
        Dal.arParams.Add(edInformation.Value);
        DataTable DT = Dal.ExecuteDBsp("Admin_SetInformation", Dal.arParams,true);
    }
    protected void btnCopyrightSave_Click(object sender, EventArgs e)
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(2);
        Dal.arParams.Add(edCopyright.Value);
        DataTable DT = Dal.ExecuteDBsp("Admin_SetInformation", Dal.arParams,true);
    }

    protected void gvPartners_DataBinding(object sender, GridViewRowEventArgs e)
    {
        AjaxControlToolkit.ConfirmButtonExtender cb = new AjaxControlToolkit.ConfirmButtonExtender();
        try
        {
            cb.TargetControlID = e.Row.FindControl("btnDeletePartner").ClientID.Replace("_","$");
            cb.OnClientCancel = "CancelClick";
            cb.ConfirmText = "Вы уверены , что хотите удалить запись ?";
            e.Row.Cells[2].Controls.Add(cb);
        }
        catch { }
    }

    protected void gvLinks_DataBinding(object sender, GridViewRowEventArgs e)
    {
        AjaxControlToolkit.ConfirmButtonExtender cb = new AjaxControlToolkit.ConfirmButtonExtender();
        try
        {
            cb.TargetControlID = e.Row.FindControl("btnDeleteLink").ClientID.Replace("_", "$");
            cb.OnClientCancel = "CancelClick";
            cb.ConfirmText = "Вы уверены , что хотите удалить запись ?";
            e.Row.Cells[2].Controls.Add(cb);
        }
        catch { }
    }
}
